<script lang="ts" setup>
import type { Ref } from 'vue';

import type { DataFormOptionsProps } from '#/components/data-form/types';

import { ref } from 'vue';

import { DataForm } from '#/components/data-form';
import { Editor } from '#/components/wangeditor';

import { useForm } from './data';

defineProps<{
    enums: Record<string, any>;
}>();

const dataFormRef = ref<InstanceType<typeof DataForm>>();

const { fields, rules, username } = useForm();

const onBeforeSubmit = (data: Ref<Record<string, any>>) => {
    // 提交前处理数据
    if (data.value.id) {
        data.value.modifyBy = username;
    } else {
        data.value.createBy = username;
    }
    return true;
};

const open = (options: DataFormOptionsProps) => {
    // 打开弹窗
    dataFormRef.value?.open(options);
};

defineExpose({ open });
</script>

<template>
    <DataForm ref="dataFormRef" :fields="fields" :enums="enums" :rules="rules" api="/system/notice" title="通知公告" fullscreen :before-submit="onBeforeSubmit">
        <template #content="{ form }">
            <Editor v-model="form.content" />
        </template>
    </DataForm>
</template>
